package com.jozen.modelbase.netwoek.util;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.signature.ObjectKey;
import com.jozen.modelbase.netwoek.base.ModuleBaseApplication;

public class GlideUtil {

//    public static void loadUrlWithDefault(String url, ImageView imageView) {
//        RequestOptions options = new RequestOptions();
//        options.centerCrop()
//                .placeholder(R.color.color_4d)
//                .error(R.color.color_4d);
//        Glide.with(MyApplication.getInstance().getApplicationContext())
//                .load(url)
//                .apply(options)
//                .into(imageView);
//    }

    /**
     * 加载本地图片
     *
     * @param url
     * @param imageView
     */
    public static void loadImageLocal(int url, ImageView imageView) {
        Glide.with(ModuleBaseApplication.getInstance().getApplicationContext())
                .load(url)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(imageView);
    }


    /**
     * 加载网络图片，加载失败显示占位图。
     *
     * @param url
     * @param imageView
     */
    public static void loadImageWithoutCache(String url, int errorId, ImageView imageView) {
        Glide.with(ModuleBaseApplication.getInstance().getApplicationContext())
                .load(url)
                .error(errorId)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(imageView);
    }

    /**
     * 加载网络图片，并且不保存数据到缓存。
     *
     * @param url
     * @param imageView
     */
    public static void loadImageWithoutCache(String url, ImageView imageView) {
        Glide.with(ModuleBaseApplication.getInstance().getApplicationContext())
                .load(url)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(imageView);
    }

    /**
     * 加载本地gif图
     *
     * @param context
     * @param drawableId
     * @param imageView
     */
    public static void loadLocalGif(Context context, int drawableId, ImageView imageView) {
        Glide.with(context)
                .asGif()
                .load(drawableId)
                .into(imageView);
    }


    /**
     * 加载网络图片，加载失败显示占位图并且使用缓存策略。
     *
     * @param url
     * @param imageView
     */
    public static void loadImageWithCache(String url, int errorId, ImageView imageView, String signature) {
        Glide.with(ModuleBaseApplication.getInstance().getApplicationContext())
                .load(url)
                .signature(new ObjectKey(signature))
                .error(errorId)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(imageView);
    }

}
